package testcases.integrate;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import testcases.common.BaseTest;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.RdcConst;
import com.roadmap.entities.Execution;
import com.roadmap.entities.LifeTask;
import com.roadmap.entities.ext.TaskExt;

public class ContentTest extends BaseTest {
	private IManager taskMgr;
	private String taskId = null;

	@Before
	public void setUp() {
		super.setUp();
		try {
			taskMgr = ManagerFactory.getInstance(RdcConst.TASK, _currentUser
					.getId());
		}
		catch (Exception e) {
			_log.fatal(e);
			fail("Unexpected exception thrown.");
		}
	}

	@After
	public void tearDown() {
		try {
			taskMgr.delete(taskId);
		}
		catch (Exception e) {
			_log.fatal(e);
			fail("Unexpected exception thrown.");
		}
	}

	@Test
	public void testCreateEntities() {
		try {
			// 创建目标

			// 创建计划

		}
		catch (Exception e) {
			fail("Unexpected exception thrown.");
		}
	}

	@Test
	public void testTaskManager() {
		try {
			// 创建任务
			LifeTask task = obtainTaskWithoutPlan();
			task.setAspirantId(_currentUser.getId());
			taskMgr.create(task);
			taskId = task.getId();

			// 找到该任务
			task = (LifeTask) taskMgr.get(taskId);
			assertTrue("failed to retrieve task back", task != null);

			TaskExt taskExt = (TaskExt) taskMgr.getEntityExt(taskId);
			assertTrue("failed to retrieve extended task back", taskExt != null);

			// 查询多任务
			Collection tasks = taskMgr.getEntitiesOfOwner();
			assertTrue("task size is 0", tasks != null && tasks.size() > 0);
			Iterator iter = tasks.iterator();
			while (iter.hasNext()) {
				String planId = ((LifeTask) iter.next()).getGoalId();
				if (planId != null) {
					Collection clt = taskMgr.getEntitiesOfParent(planId);
					assertTrue("plan's tasks size is 0", clt != null
							&& clt.size() > 0);
					break;
				}
			}

			// taskMgr.cancel(taskId);

			// 创建执行记录
			IManager execMgr = ManagerFactory.getInstance(RdcConst.EXEC,
					_currentUser.getId());
			Execution exec = obtainExec(taskId);
			execMgr.create(exec);
			List list = execMgr.getEntitiesOfParent(taskId);
			assertTrue("failed to retrieve extended task back", list != null
					&& !list.isEmpty());

			execMgr.getEntityExt(exec.getId());
			execMgr.delete(exec.getId());
		}
		catch (Exception e) {
			fail("Unexpected exception thrown.");
		}
	}
}
